<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"><title>ES6-Promise语法与原理 | XC's Blog</title><meta name="keywords" content="Ajax笔记"><meta name="author" content="XC's Blog"><meta name="copyright" content="XC's Blog"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="01. Promise作用: 解决回调地狱1.1 回调地狱: 异步回调 层层嵌套1.2 js代码分为2种: 同步(默认) 异步 同步: 按照顺序立即执行 异步: 没有顺序 延迟执行 (事件、定时器、ajax)  1.3 层层嵌套:12345678910111213141516171819202122232425262728&#x2F;&#x2F; 1.1 一级分类let xhr1 &#x3D; new XMLHttpReque"><meta property="og:type" content="article"><meta property="og:title" content="ES6-Promise语法与原理"><meta property="og:url" content="https://webxc.eu.org/post/pr67/index.html"><meta property="og:site_name" content="XC&#39;s Blog"><meta property="og:description" content="01. Promise作用: 解决回调地狱1.1 回调地狱: 异步回调 层层嵌套1.2 js代码分为2种: 同步(默认) 异步 同步: 按照顺序立即执行 异步: 没有顺序 延迟执行 (事件、定时器、ajax)  1.3 层层嵌套:12345678910111213141516171819202122232425262728&#x2F;&#x2F; 1.1 一级分类let xhr1 &#x3D; new XMLHttpReque"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp"><meta property="article:published_time" content="2023-06-11T22:30:00.000Z"><meta property="article:modified_time" content="2023-08-30T18:00:49.013Z"><meta property="article:author" content="XC&#39;s Blog"><meta property="article:tag" content="Ajax笔记"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp"><link rel="shortcut icon" href="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><link rel="canonical" href="https://webxc.eu.org/post/pr67/"><link rel="preconnect" href="//cdn.jsdelivr.net"><link rel="preconnect" href="//busuanzi.ibruce.info"><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://npm.elemecdn.com/@fortawesome/fontawesome-free@6.2.0/css/all.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.css" media="print" onload='this.media="all"'><script>const GLOBAL_CONFIG={root:"/",algolia:void 0,localSearch:{path:"/search.xml",preload:!1,languages:{hits_empty:"找不到您查询的内容：${query}"}},translate:void 0,noticeOutdate:void 0,highlight:{plugin:"highlighjs",highlightCopy:!0,highlightLang:!0,highlightHeightLimit:!1},copy:{success:"吾辈宣布,你的剪切板已被占领",error:"呀~复制失败,请在尝试下",noSupport:"对不起,你的浏览器不支持"},relativeDate:{homepage:!1,post:!1},runtime:"",date_suffix:{just:"刚刚",min:"分钟前",hour:"小时前",day:"天前",month:"个月前"},copyright:{limitCount:60,languages:{author:"作者: XC's Blog",link:"链接: ",source:"来源: XC's Blog",info:"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},lightbox:"fancybox",Snackbar:{chs_to_cht:"您已切换为繁体",cht_to_chs:"您已切换为简体",day_to_night:"您已切换为深色主题",night_to_day:"您已切换为浅色主题",bgLight:"#3b70fc",bgDark:"#121212",position:"bottom-center"},source:{justifiedGallery:{js:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.min.js",css:"https://npm.elemecdn.com/flickr-justified-gallery/dist/fjGallery.css"}},isPhotoFigcaption:!1,islazyload:!0,isAnchor:!0}</script><script id="config-diff">var GLOBAL_CONFIG_SITE={title:"ES6-Promise语法与原理",isPost:!0,isHome:!1,isHighlightShrink:!1,isToc:!0,postUpdate:"2023-08-30 18:00:49"}</script><noscript><style>#nav{opacity:1}.justified-gallery img{opacity:1}#post-meta time,#recent-posts time{display:inline!important}</style></noscript><script>(e=>{e.saveToLocal={set:function(e,t,o){if(0===o)return;const n=864e5*o,a={value:t,expiry:(new Date).getTime()+n};localStorage.setItem(e,JSON.stringify(a))},get:function(e){const t=localStorage.getItem(e);if(!t)return;const o=JSON.parse(t);if(!((new Date).getTime()>o.expiry))return o.value;localStorage.removeItem(e)}},e.getScript=e=>new Promise(((t,o)=>{const n=document.createElement("script");n.src=e,n.async=!0,n.onerror=o,n.onload=n.onreadystatechange=function(){const e=this.readyState;e&&"loaded"!==e&&"complete"!==e||(n.onload=n.onreadystatechange=null,t())},document.head.appendChild(n)})),e.activateDarkMode=function(){document.documentElement.setAttribute("data-theme","dark"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#0d0d0d")},e.activateLightMode=function(){document.documentElement.setAttribute("data-theme","light"),null!==document.querySelector('meta[name="theme-color"]')&&document.querySelector('meta[name="theme-color"]').setAttribute("content","#ffffff")};const t=saveToLocal.get("theme");"dark"===t?activateDarkMode():"light"===t&&activateLightMode();const o=saveToLocal.get("aside-status");void 0!==o&&("hide"===o?document.documentElement.classList.add("hide-aside"):document.documentElement.classList.remove("hide-aside"));/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)&&document.documentElement.classList.add("apple")})(window)</script><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mgi.css"><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/web-xc/My-blog-CSS/mg111.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.16/css/mg2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styless.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/styles.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.11/css/font.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.15/css/font2.css"><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.css" media="print" onload='this.media="all"'><link rel="stylesheet" href="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiperstyle.css" media="print" onload='this.media="all"'><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.webxc.eu.org/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image:url('https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp')"><nav id="nav"><span id="blog_name"><div class="back-home-button" tabindex="-1"><i class="back-home-button-icon fas fa-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">项目</div><div class="back-menu-list"><a class="back-menu-item" href="/" title="前往博客主页" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">主站</span></a><a class="back-menu-item" href="https://e.webxc.eu.org/" rel="external nofollow" title="前往学习笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">学习笔记</span></a><a class="back-menu-item" href="https://v.webxc.eu.org/" rel="external nofollow" title="VuePress学习文档" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">VuePress文档</span></a><a class="back-menu-item" href="https://b.webxc.eu.org/" rel="external nofollow" title="旧站Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">旧站Hexo</span></a><a class="back-menu-item" href="https://gridea.webxc.eu.org/" rel="external nofollow" title="Gridea笔记" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gridea笔记</span></a><a class="back-menu-item" href="https://c.webxc.eu.org/" rel="external nofollow" title="魔改Hexo" target="_blank" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">魔改Hexo</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">博客镜像</div><div class="back-menu-list"><a class="back-menu-item" href="https://blog.webxc.eu.org/" title="Vercel镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Vercel镜像</span></a><a class="back-menu-item" href="https://webxc.tk/" title="Netlify镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Netlify镜像</span></a><a class="back-menu-item" href="https://web-xc.gitee.io/" title="Gitee镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Gitee镜像</span></a><a class="back-menu-item" href="https://cf.webxc.eu.org/" title="Cloudflare镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Cloudflare镜像</span></a><a class="back-menu-item" href="https://render.webxc.eu.org/" title="Render镜像" target="_blank" rel="noopener nofollow" one-link-mark="yes"><img class="back-menu-item-icon" src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg"><span class="back-menu-item-text">Render镜像</span></a></div></div></div></div><a id="site-name" href="/"><div class="title">XC's Blog</div><i class="fa-solid fa-house"></i></a></span><div class="mask-name-container"><center id="name-container"><a id="page-name" href="javascript:btf.scrollToDest(0, 500)">PAGE_NAME</a></center></div><div id="menus"><div class="nav-button" id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i> <span>搜索</span></a></div><div class="nav-button" id="darkmode_navswitch"><a class="darkmode_switchbutton" type="button" title="浅色和深色模式转换" onclick="switchDarkMode()"><i class="fas fa-adjust"></i></a></div><div class="nav-button" id="nav-totop"><a class="totopbtn"><i class="fas fa-arrow-up"></i><span id="percent" onclick="btf.scrollToDest(0,500)">0</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw iconfont icon-shouye"></i> <span>首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw iconfont icon-shijian"></i> <span>时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw iconfont icon-biaoqian"></i> <span>标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw iconfont icon-fenlei"></i> <span>分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw iconfont icon-lianxiwomen"></i> <span>友联</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw iconfont icon-iconzhengli-"></i> <span>关于</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw iconfont icon-a-wangzhanchaojilianjiehulianwang"></i> <span>推荐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="http://www.inode.club/"><i class="fa-fw fa-sharp fa-solid fa-square-rss"></i> <span>程序员指南</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="http://ife.baidu.com/"><i class="fa-fw fa-brands fa-vuejs"></i> <span>前端技术学园</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.yuque.com/tademanong/ag7q4n"><i class="fa-fw fa-solid fa-users"></i> <span>前端技术圈</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.code-nav.cn/"><i class="fa-fw fa-solid fa-shop"></i> <span>编程导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://q.shanyue.tech/"><i class="fa-fw fa-brands fa-square-js"></i> <span>每日一题</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://fanyouf.gitee.io/u1word/#/h5"><i class="fa-fw fa-solid fa-headphones"></i> <span>前端必会单词</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://site.51git.cn/render/gonav/4.html"><i class="fa-fw iconfont icon-wangzhandingzhi"></i> <span>网址导航</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.webxc.eu.org/music/"><i class="fa-fw iconfont icon-musical-note"></i> <span>音乐解锁</span></a></li></ul></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">ES6-Promise语法与原理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-06-11T22:30:00.000Z" title="发表于 2023-06-11 22:30:00">2023-06-11</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-08-30T18:00:49.013Z" title="更新于 2023-08-30 18:00:49">2023-08-30</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Ajax%E7%AC%94%E8%AE%B0/">Ajax笔记</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span data-flag-title="ES6-Promise语法与原理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="twikoo_visitors"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="01-Promise作用-解决回调地狱"><a href="#01-Promise作用-解决回调地狱" class="headerlink" title="01. Promise作用: 解决回调地狱"></a>01. Promise作用: 解决回调地狱</h1><h3 id="1-1-回调地狱-异步回调-层层嵌套"><a href="#1-1-回调地狱-异步回调-层层嵌套" class="headerlink" title="1.1 回调地狱: 异步回调 层层嵌套"></a>1.1 回调地狱: 异步回调 层层嵌套</h3><h3 id="1-2-js代码分为2种-同步-默认-异步"><a href="#1-2-js代码分为2种-同步-默认-异步" class="headerlink" title="1.2 js代码分为2种: 同步(默认) 异步"></a>1.2 js代码分为2种: 同步(默认) 异步</h3><ol><li><strong>同步: 按照顺序立即执行</strong></li><li><strong>异步: 没有顺序 延迟执行 (事件、定时器、ajax)</strong></li></ol><h3 id="1-3-层层嵌套"><a href="#1-3-层层嵌套" class="headerlink" title="1.3 层层嵌套:"></a>1.3 层层嵌套:</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1.1 一级分类</span></span><br><span class="line"><span class="keyword">let</span> xhr1 = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">xhr1.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, <span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line">xhr1.<span class="title function_">send</span>()</span><br><span class="line">xhr1.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;一级&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr1.<span class="property">response</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1.2 二级分类</span></span><br><span class="line"><span class="keyword">let</span> xhr2 = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">xhr2.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, <span class="string">&#x27;http://123.57.109.30:3999/api/categorySecond?firstId=621&#x27;</span>)</span><br><span class="line">xhr2.<span class="title function_">send</span>()</span><br><span class="line">xhr2.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;二级&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr2.<span class="property">response</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 1.3 三级分类</span></span><br><span class="line"><span class="keyword">let</span> xhr3 = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">xhr3.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, <span class="string">&#x27;http://123.57.109.30:3999/api/categoryThird?secondId=622&#x27;</span>)</span><br><span class="line">xhr3.<span class="title function_">send</span>()</span><br><span class="line">xhr3.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;三级&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr3.<span class="property">response</span>))</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 1.4 浏览器刷新打印顺序会不同</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="number">666</span>)</span><br></pre></td></tr></table></figure><hr><h1 id="02-Promise语法"><a href="#02-Promise语法" class="headerlink" title="02. Promise语法"></a>02. Promise语法</h1><h3 id="2-1-调用构造函数-创建Promise实例"><a href="#2-1-调用构造函数-创建Promise实例" class="headerlink" title="2.1 调用构造函数 创建Promise实例"></a>2.1 调用构造函数 创建Promise实例</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// (resolve, reject) 是箭头函数的参数</span></span><br><span class="line"><span class="keyword">let</span> pro = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 异步代码</span></span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="comment">// resolve(1) // 成功 1是实参</span></span><br><span class="line">       <span class="title function_">reject</span>(<span class="number">2</span>) <span class="comment">// 失败</span></span><br><span class="line">    &#125;, <span class="number">500</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="2-2-调用Promise实例对象的then-x2F-catch方法"><a href="#2-2-调用Promise实例对象的then-x2F-catch方法" class="headerlink" title="2.2 调用Promise实例对象的then&#x2F;catch方法"></a>2.2 调用Promise实例对象的then&#x2F;catch方法</h3><ul><li><strong>resolve本质就是调用res res是形参 resolve(1)是实参</strong></li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// resolve和reject状态二选一的</span></span><br><span class="line">pro.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// reject失败则调用catch方法</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="2-3-Promise在创建实例时-里面代码会立即执行"><a href="#2-3-Promise在创建实例时-里面代码会立即执行" class="headerlink" title="2.3 Promise在创建实例时 里面代码会立即执行"></a>2.3 Promise在创建实例时 里面代码会立即执行</h3><ul><li><strong>Promise自己是同步的 只有then方法才是异步的</strong></li></ul><hr><h1 id="03-Promise工作原理"><a href="#03-Promise工作原理" class="headerlink" title="03. Promise工作原理"></a>03. Promise工作原理</h1><blockquote><p><strong>3.1 Promise是什么? 是ES6新增的构造函数</strong></p><p><strong>3.2 Promise作用: 解决回调地狱</strong></p><p><strong>3.3 Promise应用场景&#x2F;原理 Promise对象有三种状态:</strong></p><ol><li><strong>pending 进行中(默认状态) 所以一旦创建Promise 里面代码会立即执行</strong></li><li><strong>fuifilled 已完成</strong></li><li><strong>rejected 已失败</strong></li><li><strong>Promise相当于是一个容器 把异步代码放入容器中</strong></li><li><strong>状态只能改变一次 不管成功&#x2F;失败 都会有一个数据结果</strong></li></ol></blockquote><h3 id="4-Promise对象状态只有两种状态"><a href="#4-Promise对象状态只有两种状态" class="headerlink" title="4. Promise对象状态只有两种状态:"></a>4. Promise对象状态只有两种状态:</h3><ol><li><strong>调用resolve()方法时: 从pending变为fuifilled</strong></li><li><strong>调用reject()方法时: 从pending变为rejected</strong></li><li><strong>状态只能改变一次 不管成功&#x2F;失败 都会有一个数据结果</strong></li></ol><h3 id="5-Promise状态发生改变后-在任何时候都可以获取结果"><a href="#5-Promise状态发生改变后-在任何时候都可以获取结果" class="headerlink" title="5. Promise状态发生改变后 在任何时候都可以获取结果"></a>5. Promise状态发生改变后 在任何时候都可以获取结果</h3><ol><li><strong>Promise实例的then方法获取成功结果</strong></li><li><strong>Promise实例的catch方法获取失败结果</strong></li></ol><h3 id="6-Promise在创建实例时-里面代码会立即执行"><a href="#6-Promise在创建实例时-里面代码会立即执行" class="headerlink" title="6. Promise在创建实例时 里面代码会立即执行"></a>6. Promise在创建实例时 里面代码会立即执行</h3><ul><li><strong>Promise自己是同步的 只有then方法才是异步的</strong></li></ul><hr><h1 id="04-Promise使用链式语法解决回调地狱"><a href="#04-Promise使用链式语法解决回调地狱" class="headerlink" title="04. Promise使用链式语法解决回调地狱"></a>04. Promise使用链式语法解决回调地狱</h1><h3 id="1-创建Promise实例"><a href="#1-创建Promise实例" class="headerlink" title="1. 创建Promise实例"></a>1. 创建Promise实例</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 创建xhr对象</span></span><br><span class="line">    <span class="keyword">let</span> xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">    <span class="comment">// 设置请求方式和url</span></span><br><span class="line">    xhr.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, <span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line">    <span class="comment">// 发送ajax请求</span></span><br><span class="line">    xhr.<span class="title function_">send</span>()</span><br><span class="line">    <span class="comment">// 注册响应事件函数</span></span><br><span class="line">    xhr.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;一级&#x27;</span>)</span><br><span class="line">        <span class="keyword">let</span> res = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr.<span class="property">response</span>)</span><br><span class="line">    <span class="comment">// 1.1 Promise成功状态</span></span><br><span class="line">        <span class="title function_">resolve</span>(res)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="2-取出Promise实例对象结果"><a href="#2-取出Promise实例对象结果" class="headerlink" title="2. 取出Promise实例对象结果"></a>2. 取出Promise实例对象结果</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">p.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="3-取出Promise实例对象的函数结果"><a href="#3-取出Promise实例对象的函数结果" class="headerlink" title="3. 取出Promise实例对象的函数结果"></a>3. 取出Promise实例对象的函数结果</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">p1.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br><span class="line">p2.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br><span class="line">p3.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="4-1-三个实例对象只有url不一样-可封装成函数-帮我们创建Promise实例对象"><a href="#4-1-三个实例对象只有url不一样-可封装成函数-帮我们创建Promise实例对象" class="headerlink" title="4.1 三个实例对象只有url不一样 可封装成函数 帮我们创建Promise实例对象"></a>4.1 三个实例对象只有url不一样 可封装成函数 帮我们创建Promise实例对象</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">url</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">    <span class="comment">// 2.1 url是形参 调用时写入实参</span></span><br><span class="line">    xhr.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, url) </span><br><span class="line">    xhr.<span class="title function_">send</span>()</span><br><span class="line">    xhr.<span class="property">onload</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> res = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr.<span class="property">response</span>)</span><br><span class="line">    <span class="comment">// 2.2 Promise成功状态</span></span><br><span class="line">        <span class="title function_">resolve</span>(res)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="4-2-调用封装好的Promise实例对象函数"><a href="#4-2-调用封装好的Promise实例对象函数" class="headerlink" title="4.2 调用封装好的Promise实例对象函数"></a>4.2 调用封装好的Promise实例对象函数</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p1 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p2 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categorySecond?firstId=622&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p3 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryThird?secondId=621&#x27;</span>)</span><br></pre></td></tr></table></figure><h3 id="4-3-但打印结果还是随机的-因为只要创建代码就会立即执行"><a href="#4-3-但打印结果还是随机的-因为只要创建代码就会立即执行" class="headerlink" title="4.3 但打印结果还是随机的 因为只要创建代码就会立即执行"></a>4.3 但打印结果还是随机的 因为只要创建代码就会立即执行</h3><ul><li><strong>p1进行完后 return 返回p2的实例对象 .then可换行</strong></li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">p1.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    <span class="keyword">return</span> p2</span><br><span class="line">&#125;)</span><br><span class="line">.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123; </span><br><span class="line"><span class="comment">// p2的then</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    <span class="keyword">return</span> p3</span><br><span class="line">&#125;)</span><br><span class="line">.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// p3的then</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="5-Promise是如何解决回调地狱的呢"><a href="#5-Promise是如何解决回调地狱的呢" class="headerlink" title="5. Promise是如何解决回调地狱的呢?"></a>5. Promise是如何解决回调地狱的呢?</h3><ol><li><strong>Promise通过链式调用解决回调地狱</strong></li><li><strong>链式调用: 在上一个then里 返回下一个Promise实例 就可以继续后面的then</strong></li></ol><hr><h1 id="05-Promise的all-x2F-race方法-x2F-语法"><a href="#05-Promise的all-x2F-race方法-x2F-语法" class="headerlink" title="05. Promise的all&#x2F;race方法&#x2F;语法"></a>05. Promise的all&#x2F;race方法&#x2F;语法</h1><h3 id="1-Promise的all方法-Promise-all"><a href="#1-Promise的all方法-Promise-all" class="headerlink" title="1. Promise的all方法 Promise.all"></a>1. Promise的all方法 Promise.all</h3><ol><li><strong>把多个Promise实例合并为一个新的Promise 结果为数组</strong></li><li><strong>所有Promise要全部成功 才会执行then 只要有一个失败就会执行catch</strong></li><li><strong>then的结果为数组</strong></li><li><strong>finally 不管成功失败都执行</strong></li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">url</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">    xhr.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, url)</span><br><span class="line">    xhr.<span class="title function_">send</span>()</span><br><span class="line">    xhr.<span class="property">onloadend</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> res = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr.<span class="property">response</span>)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">        <span class="title function_">resolve</span>(res) <span class="comment">// 成功状态</span></span><br><span class="line">        </span><br><span class="line">    &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> p1 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p2 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categorySecond?firstId=621&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p3 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryThird?secondId=622&#x27;</span>)</span><br></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p = <span class="title class_">Promise</span>.<span class="title function_">all</span>([p1, p2, p3])</span><br><span class="line"><span class="comment">// 所有Promise要全部成功 才会执行then 只要有一个失败就会执行catch</span></span><br><span class="line">p.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// then的结果为数组</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">&#125;).<span class="title function_">finally</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// finally 不管成功失败都执行</span></span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;我是finally 我完事了&#x27;</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="2-Promise的race方法-Promise-race"><a href="#2-Promise的race方法-Promise-race" class="headerlink" title="2.  Promise的race方法 Promise.race"></a>2. Promise的race方法 Promise.race</h3><ol><li><strong>所有Promise谁最先成功 就会执行then 如果有失败就执行catch</strong></li><li><strong>这里res不是数组 谁最先成功就是谁</strong></li><li><strong>then的结果为最快的那个Promise</strong></li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> pp = <span class="title class_">Promise</span>.<span class="title function_">race</span>([p1, p2, p3])</span><br><span class="line">pp.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res, <span class="string">&#x27;race方法&#x27;</span>)</span><br><span class="line">&#125;).<span class="title function_">catch</span>(<span class="function"><span class="params">error</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><hr><h1 id="06-async-x2F-await异步函数"><a href="#06-async-x2F-await异步函数" class="headerlink" title="06. async&#x2F;await异步函数"></a>06. async&#x2F;await异步函数</h1><ol><li>async异步函数 async&#x2F;await 主要帮我们执行Promise</li><li>async作用: 修饰函数 让函数内部使用await</li><li>await作用: 取代then 并获取then结果</li><li>await 只能用于被async修饰的函数 否则报错 加上async变为异步函数</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p = <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">        <span class="title function_">resolve</span>(<span class="number">1</span>)</span><br><span class="line">    &#125;, <span class="number">300</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// Promise实例对象的then方法</span></span><br><span class="line">p.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="await语法"><a href="#await语法" class="headerlink" title="await语法:"></a>await语法:</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line"><span class="comment">// let res = await Promise对象</span></span><br><span class="line">    <span class="keyword">let</span> res = <span class="keyword">await</span> p</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>()</span><br><span class="line"><span class="comment">// await箭头函数语法</span></span><br><span class="line"><span class="keyword">let</span> <span class="title function_">fn1</span> = <span class="keyword">async</span> (<span class="params"></span>) =&gt; &#123;</span><br><span class="line">    <span class="keyword">let</span> res = <span class="keyword">await</span> p</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line"><span class="comment">// 2.2 let res = await 后面的代码</span></span><br><span class="line"><span class="comment">// 如果p是Promise对象 就正常执行Promise</span></span><br><span class="line">    <span class="title class_">Promise</span>(<span class="function">(<span class="params">res, rej</span>) =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// 如果p不是Promise对象 就会把代码包在Promise里面</span></span><br><span class="line">    &#125;).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// 后面的代码在then里</span></span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn1</span>()</span><br></pre></td></tr></table></figure><h3 id="6-1-使用async-x2F-await解决回调地狱练习"><a href="#6-1-使用async-x2F-await解决回调地狱练习" class="headerlink" title="6.1 使用async&#x2F;await解决回调地狱练习"></a>6.1 使用async&#x2F;await解决回调地狱练习</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 封装Promise实例对象函数</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params">url</span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">        xhr.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, url)</span><br><span class="line">        xhr.<span class="title function_">send</span>()</span><br><span class="line">        xhr.<span class="property">onloadend</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">            <span class="keyword">let</span> res = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr.<span class="property">response</span>)</span><br><span class="line">            <span class="title function_">resolve</span>(res)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2. 创建Promise实例</span></span><br><span class="line"><span class="keyword">let</span> p1 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p2 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categorySecond?firstId=621&#x27;</span>)</span><br><span class="line"><span class="keyword">let</span> p3 = <span class="title function_">fn</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryThird?secondId=622&#x27;</span>)</span><br><span class="line"><span class="comment">// 3. Promise通过链式调用解决回调地狱</span></span><br><span class="line">p1.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    <span class="keyword">return</span> p2</span><br><span class="line">&#125;).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">    <span class="keyword">return</span> p3</span><br><span class="line">&#125;).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 01. 使用async/await解决回调地狱</span></span><br><span class="line"><span class="comment">// 1.1 await右边的代码就是Promise内部的代码 .then</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">fn1</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> res1 = <span class="keyword">await</span> p1</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;一级&#x27;</span>, res1)</span><br><span class="line">    <span class="keyword">let</span> res2 = <span class="keyword">await</span> p2</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;二级&#x27;</span>, res2)</span><br><span class="line">    <span class="keyword">let</span> res3 = <span class="keyword">await</span> p3</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;三级&#x27;</span>, res3)</span><br><span class="line"><span class="comment">// 1.2 分析: await下面的代码全都是then里的代码</span></span><br><span class="line">    <span class="comment">// p1.then(res1 =&gt; &#123;</span></span><br><span class="line">    <span class="comment">//     console.log(res1)</span></span><br><span class="line">    <span class="comment">// p2.then(res2 =&gt; &#123;</span></span><br><span class="line">    <span class="comment">//     console.log(res2)</span></span><br><span class="line">    <span class="comment">// p3.then(res3 =&gt; &#123;</span></span><br><span class="line">    <span class="comment">//     console.log(res3)</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">    <span class="comment">// &#125;)</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn1</span>()</span><br></pre></td></tr></table></figure><hr><h1 id="07-了解axios底层原理-x2F-手写Promise封装xhr"><a href="#07-了解axios底层原理-x2F-手写Promise封装xhr" class="headerlink" title="07. 了解axios底层原理&#x2F;手写Promise封装xhr"></a>07. 了解axios底层原理&#x2F;手写Promise封装xhr</h1><h3 id="7-1-以前使用的axios"><a href="#7-1-以前使用的axios" class="headerlink" title="7.1 以前使用的axios"></a>7.1 以前使用的axios</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">axios.<span class="title function_">get</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="property">data</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="7-2-axios底层原理"><a href="#7-2-axios底层原理" class="headerlink" title="7.2 axios底层原理"></a>7.2 axios底层原理</h3><ol><li>axios其实就是把xhr对象包装在Promise里面</li><li>axios方法本质是返回一个Promise对象</li><li>Promise内部是原生xhr发送ajax请求</li><li>请求成功则用resolve返回给Promise对象 结果给外面的then</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p = axios.<span class="title function_">get</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p)</span><br><span class="line">p.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="property">data</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure><h3 id="7-3-手写Promise源码封装xhr"><a href="#7-3-手写Promise源码封装xhr" class="headerlink" title="7.3 手写Promise源码封装xhr"></a>7.3 手写Promise源码封装xhr</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fn = &#123;</span><br><span class="line"><span class="comment">// 2.1 封装一个get函数</span></span><br><span class="line">    <span class="title function_">get</span>(<span class="params">url</span>) &#123;</span><br><span class="line"><span class="comment">// 2.2 创建Promise实例对象</span></span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line"><span class="comment">// 2.3 这里的代码是原生xhr对象</span></span><br><span class="line">            <span class="keyword">let</span> xhr = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>()</span><br><span class="line">            xhr.<span class="title function_">open</span>(<span class="string">&#x27;get&#x27;</span>, url)</span><br><span class="line">            xhr.<span class="title function_">send</span>()</span><br><span class="line">            xhr.<span class="title function_">addEventListener</span>(<span class="string">&#x27;loadend&#x27;</span>, <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">                <span class="keyword">let</span> res = <span class="title class_">JSON</span>.<span class="title function_">parse</span>(xhr.<span class="property">response</span>)</span><br><span class="line">                <span class="title function_">resolve</span>(res)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2.4 调用get方法 会返回Promise对象</span></span><br><span class="line"><span class="keyword">let</span> p1 = fn.<span class="title function_">get</span>(<span class="string">&#x27;http://hmajax.itheima.net/api/news&#x27;</span>) </span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(p1)</span><br><span class="line"><span class="comment">// 2.5 取出Promise实例对象结果</span></span><br><span class="line">p1.<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 2.6 简写语法</span></span><br><span class="line">fn.<span class="title function_">get</span>(<span class="string">&#x27;http://hmajax.itheima.net/api/news&#x27;</span>).<span class="title function_">then</span>(<span class="function"><span class="params">res</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 2.7 使用async/await简写</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">fn1</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> res = <span class="keyword">await</span> fn.<span class="title function_">get</span>(<span class="string">&#x27;http://hmajax.itheima.net/api/news&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn1</span>()</span><br></pre></td></tr></table></figure><h3 id="7-4-使用axios异步函数依次加载-async-x2F-await"><a href="#7-4-使用axios异步函数依次加载-async-x2F-await" class="headerlink" title="7.4 使用axios异步函数依次加载-async&#x2F;await"></a>7.4 使用axios异步函数依次加载-async&#x2F;await</h3><ul><li>使用async&#x2F;await 依次加载一 二 三级的列表</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 1. 声明async异步函数</span></span><br><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> res1 = <span class="keyword">await</span> axios.<span class="title function_">get</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryfirst&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res1.<span class="property">data</span>)</span><br><span class="line"><span class="comment">// 2. await调用Promise 会取代Promise的then</span></span><br><span class="line"><span class="comment">// 3. 只要是await后面的代码都在then方法里</span></span><br><span class="line">    <span class="keyword">let</span> res2 = <span class="keyword">await</span> axios.<span class="title function_">get</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categorySecond?firstId=621&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res2.<span class="property">data</span>)</span><br><span class="line">    <span class="keyword">let</span> res3 = <span class="keyword">await</span> axios.<span class="title function_">get</span>(<span class="string">&#x27;http://123.57.109.30:3999/api/categoryThird?secondId=622&#x27;</span>)</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(res3.<span class="property">data</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>()</span><br></pre></td></tr></table></figure><h1 id="08-try-catch捕获异常"><a href="#08-try-catch捕获异常" class="headerlink" title="08. try-catch捕获异常"></a>08. try-catch捕获异常</h1><ol><li>Error对象: 内置对象 提示错误代码信息</li><li>throw错误信息: 抛出异常 让控制台变红</li><li>try-catch语法: 捕捉错误代码</li></ol><h3 id="8-1-try-throw语法"><a href="#8-1-try-throw语法" class="headerlink" title="8.1 try-throw语法"></a>8.1 try-throw语法</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">try &#123;</span><br><span class="line">// 这里代码如果遇到错误 就会执行catch</span><br><span class="line">    console.log(1)</span><br><span class="line">// 在try里写throw就会执行catch</span><br><span class="line">    throw &#x27;错误信息&#x27;</span><br><span class="line">&#125; catch (error) &#123;</span><br><span class="line">    console.log(error)</span><br><span class="line">&#125; finally &#123;</span><br><span class="line">    console.log(666)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="8-2-应用场景"><a href="#8-2-应用场景" class="headerlink" title="8.2 应用场景"></a>8.2 应用场景</h3><h3 id="8-3-用于结束forEach-面试题"><a href="#8-3-用于结束forEach-面试题" class="headerlink" title="8.3 用于结束forEach(面试题)"></a>8.3 用于结束forEach(面试题)</h3><ul><li>throw主动抛出错误 就会结束try代码 然后立即执行catch</li></ul><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> arr = [<span class="number">10</span>,<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>]</span><br><span class="line">    arr.<span class="title function_">forEach</span>(<span class="function"><span class="params">i</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (i == <span class="number">30</span>) &#123;</span><br><span class="line"><span class="comment">// throw主动抛出错误 就会结束try代码 然后立即执行catch </span></span><br><span class="line">            <span class="keyword">throw</span> <span class="number">2</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(i)</span><br><span class="line">    &#125;)</span><br><span class="line">&#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="8-4-捕捉await的错误信息"><a href="#8-4-捕捉await的错误信息" class="headerlink" title="8.4 捕捉await的错误信息"></a>8.4 捕捉await的错误信息</h3><ol><li>错误的Promise走catch 但await走不了catch 只能then</li><li>所以使用try-catch方法 配合捕捉await错误</li></ol><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="keyword">function</span> <span class="title function_">fn</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line"><span class="comment">// 错误的Promise走catch 但await走不了catch 只能then</span></span><br><span class="line"><span class="comment">// 所以使用try-catch方法 配合捕捉await错误</span></span><br><span class="line">        <span class="keyword">let</span> res = <span class="keyword">await</span> axios.<span class="title function_">get</span>(<span class="string">&#x27;http://1hmajax.itheima.net/api/news&#x27;</span>)</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(res.<span class="property">data</span>)</span><br><span class="line">    &#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(error)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="title function_">fn</span>()</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">本文作者:</span> <span class="post-copyright-info"><a href="https://webxc.eu.org">XC's Blog</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">本文链接:</span> <span class="post-copyright-info"><a href="https://webxc.eu.org/post/pr67/">https://webxc.eu.org/post/pr67/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明:</span> <span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://webxc.eu.org" target="_blank">XC's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Ajax%E7%AC%94%E8%AE%B0/">Ajax笔记</a></div><div class="post_share"><div class="social-share" data-image="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload='this.media="all"'><script src="https://npm.elemecdn.com/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.png" alt="Wechat"></a><div class="post-qr-code-desc">Wechat</div></li><li class="reward-item"><a href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="https://npm.elemecdn.com/webxc@1.0.0/lxfs/alipay.jpg" alt="Alipay"></a><div class="post-qr-code-desc">Alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/or01/"><img class="prev-cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇文章</div><div class="prev_info">Ajax第四天</div></div></a></div><div class="next-post pull-right"><a href="/post/re32/"><img class="next-cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp" onerror='onerror=null,src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="cover of next post"><div class="pagination-info"><div class="label">下一篇文章</div><div class="next_info">黑马头条-数据管理平台</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/fs67/" title="Ajax第三天"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-07</div><div class="title">Ajax第三天</div></div></a></div><div><a href="/post/or01/" title="Ajax第四天"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-08</div><div class="title">Ajax第四天</div></div></a></div><div><a href="/post/re32/" title="黑马头条-数据管理平台"><img class="cover" src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-06-15</div><div class="title">黑马头条-数据管理平台</div></div></a></div></div></div><hr><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i> <span>评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://npm.elemecdn.com/webxc@1.0.0/logo/logo.jpg" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="avatar"></div><div class="author-info__name">XC's Blog</div><div class="author-info__description">XC's Blog 记录生活和学习文章</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">95</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><div class="card-info-social-icons is-center"><a class="social-icon" href="https://tenapi.cn/qqzlk/?qq=2848141597" target="_blank" title="QQ"><i class="iconfont icon-QQ"></i></a><a class="social-icon" href="https://npm.elemecdn.com/webxc@1.0.0/lxfs/wechat.jpg" target="_blank" title="微信"><i class="iconfont icon-weixin"></i></a><a class="social-icon" href="https://github.com/web-xc" target="_blank" title="Github"><i class="iconfont icon-github"></i></a><a class="social-icon" href="https://space.bilibili.com/2134826455" target="_blank" title="BiliBili"><i class="iconfont icon-bilibili"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=323682437" target="_blank" title="网易云音乐"><i class="iconfont icon-wyy"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">原网站因使用.ml域名被回收, 再加上本人因学习原因, 没怎么打理博客, 使用了https://webxc.eu.org域名来维持博客运行, 如果有加友联的朋友们, 去友联页看信息即可, 如果原来有加过的, 麻烦在原来域名后缀改一下(.ml ==> .eu.org)就行, 博客写的太乱了, 没时间打理, 兄弟们谅解😁😁~~</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#01-Promise%E4%BD%9C%E7%94%A8-%E8%A7%A3%E5%86%B3%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1"><span class="toc-number">1.</span> <span class="toc-text">01. Promise作用: 解决回调地狱</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1-%E5%BC%82%E6%AD%A5%E5%9B%9E%E8%B0%83-%E5%B1%82%E5%B1%82%E5%B5%8C%E5%A5%97"><span class="toc-number">1.0.1.</span> <span class="toc-text">1.1 回调地狱: 异步回调 层层嵌套</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-js%E4%BB%A3%E7%A0%81%E5%88%86%E4%B8%BA2%E7%A7%8D-%E5%90%8C%E6%AD%A5-%E9%BB%98%E8%AE%A4-%E5%BC%82%E6%AD%A5"><span class="toc-number">1.0.2.</span> <span class="toc-text">1.2 js代码分为2种: 同步(默认) 异步</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E5%B1%82%E5%B1%82%E5%B5%8C%E5%A5%97"><span class="toc-number">1.0.3.</span> <span class="toc-text">1.3 层层嵌套:</span></a></li></ol></li></ol><li class="toc-item toc-level-1"><a class="toc-link" href="#02-Promise%E8%AF%AD%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">02. Promise语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E8%B0%83%E7%94%A8%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0-%E5%88%9B%E5%BB%BAPromise%E5%AE%9E%E4%BE%8B"><span class="toc-number">2.0.1.</span> <span class="toc-text">2.1 调用构造函数 创建Promise实例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-%E8%B0%83%E7%94%A8Promise%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E7%9A%84then-x2F-catch%E6%96%B9%E6%B3%95"><span class="toc-number">2.0.2.</span> <span class="toc-text">2.2 调用Promise实例对象的then&#x2F;catch方法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-Promise%E5%9C%A8%E5%88%9B%E5%BB%BA%E5%AE%9E%E4%BE%8B%E6%97%B6-%E9%87%8C%E9%9D%A2%E4%BB%A3%E7%A0%81%E4%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C"><span class="toc-number">2.0.3.</span> <span class="toc-text">2.3 Promise在创建实例时 里面代码会立即执行</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#03-Promise%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-number">3.</span> <span class="toc-text">03. Promise工作原理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Promise%E5%AF%B9%E8%B1%A1%E7%8A%B6%E6%80%81%E5%8F%AA%E6%9C%89%E4%B8%A4%E7%A7%8D%E7%8A%B6%E6%80%81"><span class="toc-number">3.0.1.</span> <span class="toc-text">4. Promise对象状态只有两种状态:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Promise%E7%8A%B6%E6%80%81%E5%8F%91%E7%94%9F%E6%94%B9%E5%8F%98%E5%90%8E-%E5%9C%A8%E4%BB%BB%E4%BD%95%E6%97%B6%E5%80%99%E9%83%BD%E5%8F%AF%E4%BB%A5%E8%8E%B7%E5%8F%96%E7%BB%93%E6%9E%9C"><span class="toc-number">3.0.2.</span> <span class="toc-text">5. Promise状态发生改变后 在任何时候都可以获取结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-Promise%E5%9C%A8%E5%88%9B%E5%BB%BA%E5%AE%9E%E4%BE%8B%E6%97%B6-%E9%87%8C%E9%9D%A2%E4%BB%A3%E7%A0%81%E4%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C"><span class="toc-number">3.0.3.</span> <span class="toc-text">6. Promise在创建实例时 里面代码会立即执行</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#04-Promise%E4%BD%BF%E7%94%A8%E9%93%BE%E5%BC%8F%E8%AF%AD%E6%B3%95%E8%A7%A3%E5%86%B3%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1"><span class="toc-number">4.</span> <span class="toc-text">04. Promise使用链式语法解决回调地狱</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%88%9B%E5%BB%BAPromise%E5%AE%9E%E4%BE%8B"><span class="toc-number">4.0.1.</span> <span class="toc-text">1. 创建Promise实例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%8F%96%E5%87%BAPromise%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E7%BB%93%E6%9E%9C"><span class="toc-number">4.0.2.</span> <span class="toc-text">2. 取出Promise实例对象结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-%E5%8F%96%E5%87%BAPromise%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E7%9A%84%E5%87%BD%E6%95%B0%E7%BB%93%E6%9E%9C"><span class="toc-number">4.0.3.</span> <span class="toc-text">3. 取出Promise实例对象的函数结果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E4%B8%89%E4%B8%AA%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E5%8F%AA%E6%9C%89url%E4%B8%8D%E4%B8%80%E6%A0%B7-%E5%8F%AF%E5%B0%81%E8%A3%85%E6%88%90%E5%87%BD%E6%95%B0-%E5%B8%AE%E6%88%91%E4%BB%AC%E5%88%9B%E5%BB%BAPromise%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1"><span class="toc-number">4.0.4.</span> <span class="toc-text">4.1 三个实例对象只有url不一样 可封装成函数 帮我们创建Promise实例对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-%E8%B0%83%E7%94%A8%E5%B0%81%E8%A3%85%E5%A5%BD%E7%9A%84Promise%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1%E5%87%BD%E6%95%B0"><span class="toc-number">4.0.5.</span> <span class="toc-text">4.2 调用封装好的Promise实例对象函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-3-%E4%BD%86%E6%89%93%E5%8D%B0%E7%BB%93%E6%9E%9C%E8%BF%98%E6%98%AF%E9%9A%8F%E6%9C%BA%E7%9A%84-%E5%9B%A0%E4%B8%BA%E5%8F%AA%E8%A6%81%E5%88%9B%E5%BB%BA%E4%BB%A3%E7%A0%81%E5%B0%B1%E4%BC%9A%E7%AB%8B%E5%8D%B3%E6%89%A7%E8%A1%8C"><span class="toc-number">4.0.6.</span> <span class="toc-text">4.3 但打印结果还是随机的 因为只要创建代码就会立即执行</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Promise%E6%98%AF%E5%A6%82%E4%BD%95%E8%A7%A3%E5%86%B3%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1%E7%9A%84%E5%91%A2"><span class="toc-number">4.0.7.</span> <span class="toc-text">5. Promise是如何解决回调地狱的呢?</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#05-Promise%E7%9A%84all-x2F-race%E6%96%B9%E6%B3%95-x2F-%E8%AF%AD%E6%B3%95"><span class="toc-number">5.</span> <span class="toc-text">05. Promise的all&#x2F;race方法&#x2F;语法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Promise%E7%9A%84all%E6%96%B9%E6%B3%95-Promise-all"><span class="toc-number">5.0.1.</span> <span class="toc-text">1. Promise的all方法 Promise.all</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-Promise%E7%9A%84race%E6%96%B9%E6%B3%95-Promise-race"><span class="toc-number">5.0.2.</span> <span class="toc-text">2. Promise的race方法 Promise.race</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#06-async-x2F-await%E5%BC%82%E6%AD%A5%E5%87%BD%E6%95%B0"><span class="toc-number">6.</span> <span class="toc-text">06. async&#x2F;await异步函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#await%E8%AF%AD%E6%B3%95"><span class="toc-number">6.0.1.</span> <span class="toc-text">await语法:</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-1-%E4%BD%BF%E7%94%A8async-x2F-await%E8%A7%A3%E5%86%B3%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1%E7%BB%83%E4%B9%A0"><span class="toc-number">6.0.2.</span> <span class="toc-text">6.1 使用async&#x2F;await解决回调地狱练习</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#07-%E4%BA%86%E8%A7%A3axios%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86-x2F-%E6%89%8B%E5%86%99Promise%E5%B0%81%E8%A3%85xhr"><span class="toc-number">7.</span> <span class="toc-text">07. 了解axios底层原理&#x2F;手写Promise封装xhr</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#7-1-%E4%BB%A5%E5%89%8D%E4%BD%BF%E7%94%A8%E7%9A%84axios"><span class="toc-number">7.0.1.</span> <span class="toc-text">7.1 以前使用的axios</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-2-axios%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">7.0.2.</span> <span class="toc-text">7.2 axios底层原理</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-3-%E6%89%8B%E5%86%99Promise%E6%BA%90%E7%A0%81%E5%B0%81%E8%A3%85xhr"><span class="toc-number">7.0.3.</span> <span class="toc-text">7.3 手写Promise源码封装xhr</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-4-%E4%BD%BF%E7%94%A8axios%E5%BC%82%E6%AD%A5%E5%87%BD%E6%95%B0%E4%BE%9D%E6%AC%A1%E5%8A%A0%E8%BD%BD-async-x2F-await"><span class="toc-number">7.0.4.</span> <span class="toc-text">7.4 使用axios异步函数依次加载-async&#x2F;await</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#08-try-catch%E6%8D%95%E8%8E%B7%E5%BC%82%E5%B8%B8"><span class="toc-number">8.</span> <span class="toc-text">08. try-catch捕获异常</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#8-1-try-throw%E8%AF%AD%E6%B3%95"><span class="toc-number">8.0.1.</span> <span class="toc-text">8.1 try-throw语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-2-%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF"><span class="toc-number">8.0.2.</span> <span class="toc-text">8.2 应用场景</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-3-%E7%94%A8%E4%BA%8E%E7%BB%93%E6%9D%9FforEach-%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="toc-number">8.0.3.</span> <span class="toc-text">8.3 用于结束forEach(面试题)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-4-%E6%8D%95%E6%8D%89await%E7%9A%84%E9%94%99%E8%AF%AF%E4%BF%A1%E6%81%AF"><span class="toc-number">8.0.4.</span> <span class="toc-text">8.4 捕捉await的错误信息</span></a></li></ol></li></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/ds2s/" title="整个前端面试题"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="整个前端面试题"></a><div class="content"><a class="title" href="/post/ds2s/" title="整个前端面试题">整个前端面试题</a><time datetime="2023-08-31T02:00:00.000Z" title="发表于 2023-08-31 02:00:00">2023-08-31</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/ds23/" title="常见前端面试题"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="常见前端面试题"></a><div class="content"><a class="title" href="/post/ds23/" title="常见前端面试题">常见前端面试题</a><time datetime="2023-08-28T18:30:00.000Z" title="发表于 2023-08-28 18:30:00">2023-08-28</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/fe33/" title="硅谷甄选运营平台"><img src="https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png" onerror='this.onerror=null,this.src="https://npm.elemecdn.com/webxc@1.0.2/banner/404.gif"' alt="硅谷甄选运营平台"></a><div class="content"><a class="title" href="/post/fe33/" title="硅谷甄选运营平台">硅谷甄选运营平台</a><time datetime="2023-07-26T03:55:00.000Z" title="发表于 2023-07-26 03:55:00">2023-07-26</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2021 - 2023 By XC's Blog</div><div class="framework-info"><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/?keyword=20235221">萌ICP备20235221号</a><span class="footer-separator">|</span><span></span> <a target="_blank" rel="noopener" href="https://icp.gov.moe/">萌ICP查询</a><div id="running-time"></div></div><script>setInterval((()=>{let e=Math.round(new Date("2021-11-21 01:35:06").getTime()/1e3),t=Math.round(((new Date).getTime()+288e5)/1e3)-e,n=new Array(0,0,0,0,0);t>=31536e3&&(n[0]=parseInt(t/31536e3),t%=31536e3),t>=86400&&(n[1]=parseInt(t/86400),t%=86400),t>=3600&&(n[2]=parseInt(t/3600),t%=3600),t>=60&&(n[3]=parseInt(t/60),t%=60),t>0&&(n[4]=t),currentTimeHtml="本站已安全运行 "+n[0]+" 年 "+n[1]+" 天 "+n[2]+" 时 "+n[3]+" 分 "+n[4]+" 秒",document.getElementById("running-time").innerHTML=currentTimeHtml}),1e3)</script></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i><span id="percent">0<span>%</span></span></button><button id="go-down" type="button" title="直达底部" onclick="btf.scrollToDest(document.body.scrollHeight,500)"><i class="fas fa-arrow-down"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i> <span>正在载入文件,请稍后</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"></div></div><hr><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/utils.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/main.js"></script><script src="https://npm.elemecdn.com/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/instant.page/instantpage.js" type="module"></script><script src="https://npm.elemecdn.com/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/node-snackbar/dist/snackbar.min.js"></script><script src="https://npm.elemecdn.com/hexo-theme-butterfly/source/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{const e=document.querySelectorAll("#article-container .mermaid-wrap");if(e.length){window.runMermaid=()=>{window.loadMermaid=!0;const t="dark"===document.documentElement.getAttribute("data-theme")?"dark":"default";Array.from(e).forEach(((e,n)=>{const d=e.firstElementChild,r="mermaid-"+n,m="%%{init:{ 'theme':'"+t+"'}}%%\n"+d.textContent;mermaid.mermaidAPI.render(r,m,(e=>{d.insertAdjacentHTML("afterend",e)}))}))};const t=()=>{window.loadMermaid?runMermaid():getScript("https://npm.elemecdn.com/mermaid/dist/mermaid.min.js").then(runMermaid)};window.pjax?t():document.addEventListener("DOMContentLoaded",t)}})()</script><script>(()=>{const o=()=>{twikoo.init(Object.assign({el:"#twikoo-wrap",envId:"https://blog2.webxc.eu.org/",region:"",onCommentLoaded:function(){btf.loadLightbox(document.querySelectorAll("#twikoo .tk-content img:not(.tk-owo-emotion)"))}},null))},t=()=>{"object"!=typeof twikoo?getScript("https://npm.elemecdn.com/twikoo@1.6.16/dist/twikoo.all.min.js").then(o):setTimeout(o,0)};t()})()</script></div><script data-pjax="false" defer src="https://npm.elemecdn.com/webxc@1.0.16/css/style.js"></script><script defer src="https://npm.elemecdn.com/hexo-theme-butterfly@4.4.0/source/js/search/local-search.js"></scrip><script src="https://npm.elemecdn.com/butterfly-extsrc/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!0,POWERMODE.mobile=!0,document.body.addEventListener("input",POWERMODE)</script><script src="https://npm.elemecdn.com/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors=["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax",".tk-comments-container"];var pjax=new Pjax({elements:'a:not([target="_blank"])',selectors:pjaxSelectors,cacheBust:!1,analytics:!1,scrollRestoration:!1});document.addEventListener("pjax:send",(function(){if(window.tocScrollFn&&window.removeEventListener("scroll",window.tocScrollFn),window.scrollCollect&&window.removeEventListener("scroll",scrollCollect),"object"==typeof preloader&&preloader.initLoading(),document.getElementById("rightside").style.cssText="opacity: ''; transform: ''",window.aplayers)for(let e=0;e<window.aplayers.length;e++)window.aplayers[e].options.fixed||window.aplayers[e].destroy();"object"==typeof typed&&typed.destroy();const e=document.body.classList;e.contains("read-mode")&&e.remove("read-mode"),"object"==typeof disqusjs&&disqusjs.destroy()})),document.addEventListener("pjax:complete",(function(){window.refreshFn(),document.querySelectorAll("script[data-pjax]").forEach((e=>{const t=document.createElement("script"),o=e.text||e.textContent||e.innerHTML||"";Array.from(e.attributes).forEach((e=>t.setAttribute(e.name,e.value))),t.appendChild(document.createTextNode(o)),e.parentNode.replaceChild(t,e)})),GLOBAL_CONFIG.islazyload&&window.lazyLoadInstance.update(),"function"==typeof chatBtnFn&&chatBtnFn(),"function"==typeof panguInit&&panguInit(),"function"==typeof gtag&&gtag("config","",{page_path:window.location.pathname}),"object"==typeof _hmt&&_hmt.push(["_trackPageview",window.location.pathname]),"function"==typeof loadMeting&&document.getElementsByClassName("aplayer").length&&loadMeting(),"object"==typeof Prism&&Prism.highlightAll(),"object"==typeof preloader&&preloader.endLoading()})),document.addEventListener("pjax:error",(e=>{404===e.request.status&&pjax.loadUrl("/404.html")}))</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script data-pjax>function butterfly_swiper_injector_config(){var s=document.getElementById("recent-posts");console.log("已挂载butterfly_swiper"),s.insertAdjacentHTML("afterbegin",'<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-08-31</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ds2s/&quot;);" href="javascript:void(0);" alt="">整个前端面试题</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-08-28</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ds23/&quot;);" href="javascript:void(0);" alt="">常见前端面试题</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-26</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe33/&quot;);" href="javascript:void(0);" alt="">硅谷甄选运营平台</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-17</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ew32/&quot;);" href="javascript:void(0);" alt="">Vue第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-16</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/aw92/&quot;);" href="javascript:void(0);" alt="">Vue第二天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-14</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/ou67/&quot;);" href="javascript:void(0);" alt="">Vue第一天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/vue.png);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-07-05</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fn32/&quot;);" href="javascript:void(0);" alt="">Vue框架笔记</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-21</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe11/&quot;);" href="javascript:void(0);" alt="">Vue前置JS功底</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/promise.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-11</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/pr67/&quot;);" href="javascript:void(0);" alt="">ES6-Promise语法与原理</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-08</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/or01/&quot;);" href="javascript:void(0);" alt="">Ajax第四天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://cdn1.tianli0.top/gh/web-xc/iMG/postss/ajax.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-06-07</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fs67/&quot;);" href="javascript:void(0);" alt="">Ajax第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-11</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe66/&quot;);" href="javascript:void(0);" alt="">JS进阶第四天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-10</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/fe99/&quot;);" href="javascript:void(0);" alt="">JS进阶第三天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc/cover/js1.webp);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2023-05-05</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/us87/&quot;);" href="javascript:void(0);" alt="">JS进阶第二天</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover15.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-10-31</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/2db5/&quot;);" href="javascript:void(0);" alt="">移动开发-Flex布局</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover16.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-14</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/1c7e/&quot;);" href="javascript:void(0);" alt="">移动开发-媒体查询</a><div class="blog-slider__text">点击查看</div></div></div><div class="blog-slider__item swiper-slide" style="background:url(https://npm.elemecdn.com/webxc@1.0.12/cover/cover17.jpg);border-radius:12px;opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><div class="blog-slider__content"><span class="blog-slider__code">2022-11-17</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;post/7fc/&quot;);" href="javascript:void(0);" alt="">移动开发-响应式</a><div class="blog-slider__text">点击查看</div></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div><div class="swiper-button-prev"></div><div class="swiper-button-next"></div></div></div>')}for(var elist="null".split(","),cpage=location.pathname,epage="all",flag=0,i=0;i<elist.length;i++)cpage.includes(elist[i])&&flag++;("all"===epage&&0==flag||epage===cpage)&&butterfly_swiper_injector_config()</script><script defer src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/webxc@1.0.12/css/swiper/swiper_init.js"></script></body></html>